const app = getApp();
import {
  post
} from '../../utils/request';
import {
  bMapTransqqMap
} from '../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: null,
    list: [],
    loading: false,

    globalData: app.globalData,

    fixedHeight: app.globalData.titleHeight + app.globalData.statusBarHeight,
    fixedhd: false,

    slide: {
      indicatorDots: false,
      vertical: false,
      autoplay: true,
      interval: 2000,
      duration: 500,
    },
  },

  // 获取列表数据
  getDetail(id) {
    post(`/mch/info/detail`, {
        tenantCode: app.globalData.tenantCode,
        id
      })
      .then(resp => {
        if (resp.code === 0) {
          this.setData({
            detail: resp.data
          });
        }
      });
  },

  // 获取列表数据
  getList(id) {
    wx.showLoading({
      title: '正在加载',
    });
    this.setData({
      loading: true
    });
    post(`/mch/info/goods/list`, {
        tenantCode: app.globalData.tenantCode,
        id
      })
      .then(resp => {
        wx.hideLoading();
        this.setData({
          loading: false
        });
        if (resp.code === 0) {
          this.setData({
            list: resp.data.filter(d => d.items.length > 0)
          });
        }
      });
  },

  goBack() {
    wx.navigateBack();
  },

  onHome() {
    wx.reLaunch({
      url: '/pages/home/index',
    });
  },

  onMobile() {
    if (this.data.detail.serviceTel) {
      wx.makePhoneCall({
        phoneNumber: this.data.detail.serviceTel,
      });
      return
    }
  },

  onLocation() {
    if (this.data.detail.lng && this.data.detail.lat) {
      let local = bMapTransqqMap(this.data.detail.lng, this.data.detail.lat);
      wx.openLocation({
        longitude: local.longitude,
        latitude: local.latitude,
        address: this.data.detail.addr,
      });
    }
  },

  // 滚动监听
  changeScroll(e) {
    // 获取距离顶部的距离
    let scrollTop = e.detail.scrollTop;

    if (scrollTop >= this.data.fixedHeight) {
      if (!this.data.fixedhd) {
        this.setData({
          fixedhd: true,
        });
      }
    } else {
      if (this.data.fixedhd) {
        this.setData({
          fixedhd: false,
        });
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    wx.setBackgroundColor({
      backgroundColor: "#f6f6f6"
    });

    let options = app.getPageOptions();
    this.getList(options.id);
    this.getDetail(options.id);
  },

  // 详情页面
  onDetail(e) {
    let {
      info
    } = e.currentTarget.dataset;
    // 商品
    if (info.type === 0) {
      wx.navigateTo({
        url: `/pages/goods/detail?id=${info.id}`,
      });
    }
    // 服务
    if (info.type === 1) {
      wx.navigateTo({
        url: `/pages/serve/detail?id=${info.id}`,
      });
    }
    // 套餐
    if (info.type === 2) {
      wx.navigateTo({
        url: `/pages/pkg/detail?id=${info.id}`,
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: this.data.detail.name,
      path: `/pages/mch/detail?id=${this.data.detail.id}`,
      imageUrl: this.data.detail.cover
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareTimeline() {
    return {
      title: this.data.detail.name,
      path: `/pages/mch/detail?id=${this.data.detail.id}`,
      imageUrl: this.data.detail.cover
    }
  },

});